﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class ProductCartDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the ProductCart table.
		/// </summary>
		public virtual int Insert(ProductCartInfo productCartInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProductId", productCartInfo.ProductId),
				new SqlParameter("@Quantity", productCartInfo.Quantity),
				new SqlParameter("@TotalPrice", productCartInfo.TotalPrice),
				new SqlParameter("@CreatedDate", productCartInfo.CreatedDate),
				new SqlParameter("@MemberId", productCartInfo.MemberId)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductCart_Insert", parameters);
		}

        public virtual void Insert_giohang(ProductCartInfo_giohang productCartInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@OrderId", productCartInfo.CartId),
                new SqlParameter("@ProductName", productCartInfo.ProductName),
                new SqlParameter("@Quantity", productCartInfo.Quantity),
                new SqlParameter("@Price", productCartInfo.TotalPrice),
                new SqlParameter("@CreatedDate", productCartInfo.CreatedDate),
                new SqlParameter("@Dealxu", productCartInfo.Dealxu)
            };
            //   return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductCart_add", parameters);
            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_add", parameters);
        }

		/// <summary>
		/// Updates a record in the ProductCart table.
		/// </summary>
		public virtual void Update(ProductCartInfo productCartInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CartId", productCartInfo.CartId),
				new SqlParameter("@ProductId", productCartInfo.ProductId),
				new SqlParameter("@Quantity", productCartInfo.Quantity),
				new SqlParameter("@TotalPrice", productCartInfo.TotalPrice),
				new SqlParameter("@CreatedDate", productCartInfo.CreatedDate),
				new SqlParameter("@MemberId", productCartInfo.MemberId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductCart_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the ProductCart table by its primary key.
		/// </summary>
		public virtual void Delete(int cartId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CartId", cartId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductCart_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the ProductCart table.
		/// </summary>
		public virtual ProductCartInfo Select(int cartId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CartId", cartId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductCart_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeProductCartInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the ProductCart table.
		/// </summary>
		public CHRTList<ProductCartInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductCart_SelectAll"))
			{
				CHRTList<ProductCartInfo> productCartInfoList = new CHRTList<ProductCartInfo>();
				while (dataReader.Read())
				{
					ProductCartInfo productCartInfo = MakeProductCartInfo(dataReader);
					productCartInfoList.Add(productCartInfo);
				}

				return productCartInfoList;
			}
		}
     



        
        public int GetCount( DateTime createOnFrom, DateTime createOnTo)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@CreatedOnFrom",createOnFrom),
                                                new SqlParameter("@CreatedOnTo",createOnTo)                                               
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductCart_GetCount", parameters);

        }
        public DataTable SearchAll(DateTime createdOnFrom, DateTime createdOnTo ,int from, int to)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@CreatedOnFrom",createdOnFrom),
                                              new SqlParameter("@CreatedOnTo",createdOnTo),
                                              new SqlParameter("@From",from),
                                              new SqlParameter("@To",to)
		};
            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "ProductCart_SearchAll", parameters))
            {
                if (dt.Rows.Count > 0)
                    return dt;
                else
                {
                    return null;
                }
            }
        }

        
        public DataSet ListProductCartPage(int cartId, int productId, int memberId,DateTime createOnFrom, DateTime createOnTo, int currPage, int recodperPage, int pageSize, int total, string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@CartId", cartId),
                                              new SqlParameter("@ProductId", productId),
                                              new SqlParameter("@MemberId", memberId), 
                                              new SqlParameter("@CreatedOnFrom",createOnFrom),
                                              new SqlParameter("@CreatedOnTo",createOnTo),
                                              new SqlParameter("@currPage", currPage),
                                              new SqlParameter("@recodperPage", recodperPage),
                                              new SqlParameter("@pageSize", pageSize), 
                                              new SqlParameter("@total", total),
                                              new SqlParameter("@link", link)
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "ProductCart_search_Page", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }

        public virtual int SelectCount( bool isTotalRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
				new SqlParameter("@isTotalRow", true.ToString())
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Sp_ProductCart_Search", parameters);
        }

        public virtual DataTable Search(int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", false.ToString())
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Sp_ProductCart_Search", parameters);
        }

		/// <summary>
		/// Creates a new instance of the ProductCart class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual ProductCartInfo MakeProductCartInfo(SqlDataReader dataReader)
		{
			ProductCartInfo productCartInfo = new ProductCartInfo();
			productCartInfo.CartId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTCART.CARTID, 0);
			productCartInfo.ProductId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTCART.PRODUCTID, 0);
			productCartInfo.Quantity = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTCART.QUANTITY, 0);
			productCartInfo.TotalPrice = SqlClientUtility.GetFloat(dataReader,DbConstants.PRODUCTCART.TOTALPRICE, 0.0F);
			productCartInfo.CreatedDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.PRODUCTCART.CREATEDDATE, DateTime.Now);
			productCartInfo.MemberId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTCART.MEMBERID, 0);

			return productCartInfo;
		}

		#endregion
	}
}
